package com.micolor.logr.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.micolor.logr.demo.entity.UserInfo;
import com.micolor.logr.demo.mapper.UserMapper;
import com.micolor.logr.demo.service.IUserService;
import com.micolor.logr.demo.service.log.dao.UserLogDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author EvanGe
 * @Type UserServiceImpl.java
 * @Desc
 * @date 2024/7/8 11:05
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserInfo> implements IUserService {

    @Autowired
    UserLogDao userLogDao;
    @Override
    public UserInfo saveUserInfo(UserInfo userInfo) throws Exception {
        userLogDao.saveUserInfo(userInfo);
        return userInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserInfo updateUserInfo(UserInfo userInfo) throws Exception{
        //查询原来的用户数据
        LambdaQueryWrapper<UserInfo> qwUser = new LambdaQueryWrapper<>();
        qwUser.eq(UserInfo::getUserId,userInfo.getUserId());
        UserInfo oldUser =  this.getOne(qwUser);
        userLogDao.updateUserInfo(oldUser,userInfo);
        if(true){
            throw new Exception("llll");
        }
        return userInfo;
    }

    @Override
    public UserInfo deleteUserInfo(UserInfo userInfo) {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfo::getUserId,userInfo.getUserId());
        UserInfo u =   this.getOne(queryWrapper);
        userLogDao.deleteUserInfo(u);
        return userInfo;
    }
}
